package cn.stylefeng.guns.modular.info.service;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.modular.info.entity.League;
import cn.stylefeng.guns.modular.info.entity.LeagueTeam;
import cn.stylefeng.guns.modular.info.mapper.GameMapper;
import cn.stylefeng.guns.modular.info.mapper.LeagueMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @program: guns-vip
 * @description:
 * @author: xiesm
 * @create: 2019-12-30 17:57
 **/
@Service
public class InfoService extends ServiceImpl<LeagueMapper, League> {

    @Autowired
    private GameMapper gameMapper;

    public List<League> findLeagueList(League league) {
        QueryWrapper<League> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("id", 0);
        if (league.getName() != null) {
            queryWrapper.like("name", league.getName());
        }
        queryWrapper.orderByAsc("priority");
        List<League> leagues = this.baseMapper.selectList(queryWrapper);
        List<Map<String, Object>> ownLists = gameMapper.groupGameOwnGoalCount();
        Map<Integer, Integer> map = getMap(ownLists); //乌龙球

        List<Map<String, Object>> halfList = gameMapper.groupGameHalfGoalCount();
        Map<Integer, Integer> halfMap = getMap(halfList); //上下半场都得分

        leagues.stream().forEach(league2 -> {
            league2.setOwnGoalNum(map.get(league2.getId()) != null ? map.get(league2.getId()) : 0);
            league2.setHalfGoalNum(halfMap.get(league2.getId()) != null ? halfMap.get(league2.getId()) : 0);
        });
        return leagues;
    }

    private Map<Integer, Integer> getMap(List<Map<String, Object>> lists) {
        Map<Integer, Integer> map = new HashMap<>();
        lists.stream().forEach(list -> {
            Set<String> set = list.keySet();
            String[] id = new String[1];
            String[] count = new String[1];
            set.stream().forEach(key -> {
                if (Objects.equals("id", key)) {
                    id[0] = list.get(key).toString();
                }
                if (Objects.equals("count", key)) {
                    count[0] = list.get(key).toString();
                }
            });
            map.put(Integer.valueOf(id[0]), Integer.valueOf(count[0]));
        });
        return map;
    }

    public static void setSizeColumn(Sheet sheet, int size) {
        short cellNum = sheet.getRow(0).getLastCellNum();
        for (int i = 0; i < cellNum; i++) {
            sheet.autoSizeColumn(i);
            sheet.setColumnWidth(i,sheet.getColumnWidth(i)*17/10);
        }
    }


}
